package com.yy.crypto.common.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象
 */

@Data
public class DResult<T> implements Serializable {
    /**
     * 通信数据
     */
    private T data;
    /**
     * 通信状态
     */
    private String code = ResultCode.SUCCESS.getCode();
    /**
     * 通信描述
     */
    private String msg = ResultCode.SUCCESS.getMsg();

    /**
     * 通过静态方法获取实例
     */
    public static <T> DResult<T> of(T data) {
        return new DResult<>(data);
    }

    public static <T> DResult<T> of(T data, String code) {
        return new DResult<>(data, code);
    }

    public static <T> DResult<T> of(String code, String msg) {
        return new DResult<>(code, msg);
    }

    public static <T> DResult<T> of(T data, String code, String msg) {
        return new DResult<>(data, code, msg);
    }

    public static <T> DResult<T> failed() {
        return new DResult(null, ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMsg());
    }

    public static <T> DResult<T> failed(String msg) {
        return new DResult(null, ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg);
    }

    public static <T> DResult<T> failed(String code, String msg) {
        return new DResult(null, code, msg);
    }

    @Deprecated
    public DResult() {

    }

    private DResult(T data) {
        this.data = data;
    }

    private DResult(T data, String code) {
        this.data = data;
        this.code = code;
    }

    private DResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private DResult(T data, String code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> DResult<T> failed(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null);
    }

    private static <T> DResult<T> result(IResultCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMsg(), data);
    }

    private static <T> DResult<T> result(String code, String msg, T data) {
        DResult<T> result = new DResult<T>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> DResult<T> success(T data, Long total) {
        DResult<T> result = new DResult();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> DResult<T> success() {
        DResult<T> result = new DResult();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

}
